//
//  ElectricityDetailViewController.m
//  EasyToo
//
//  Created by linshengqin on 13-8-24.
//
//

#import "ElectricityDetailViewController.h"
#import "ElectricityDetail2ViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "DeviceUtil.h"
#import "ElectricFeeInfo.h"
#import "ElectricityInfoViewController.h"
#define HEIGTH 40

@interface ElectricityDetailViewController ()

@end

@implementation ElectricityDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    electricity_model =[ElectricityModel sharedata];
	// Do any additional setup after loading the view.
     [self.view setBackgroundColor:BackgroundColor];
    for (int i = 0; i < [electricity_model.electricFeeInfoArray count]; i++)
    {
//        cellLabelArray = [[NSMutableArray alloc]initWithObjects:@"2013-07-04",@"200", nil];
//        cellLabelArray2 = [[NSMutableArray alloc]initWithObjects:@"2013-07-05",@"250", nil];
        
    }
    NSLog(@"lanzhiyuan================= electricfeeinfo:%@\n", electricity_model.electricFeeInfoArray);
    [self initMainView];
    [self initTableView:electricity_model.electricFeeInfoArray];
    [self initLabel:[electricity_model.electricFeeInfoArray count]*40];
    [self initButton:[electricity_model.electricFeeInfoArray count]*40];
}
-(void)initMainView
{
    _bgScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [DeviceUtil getScreenWidth], [DeviceUtil getScreenHeight])];
    [self.view addSubview:_bgScrollView];
    _bgScrollView.contentSize = CGSizeMake(320, 700);
    _bgScrollView.showsVerticalScrollIndicator = YES;
    
}

-(void) initButton:(int)distance
{
    //下一步按钮
    UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [nextButton setFrame:CGRectMake(20, distance+140, 280, 40)];
    
    [nextButton setTitle:@"下一步" forState:UIControlStateNormal];        //设置button在没有选中的时候显示的字体
    
    nextButton.titleLabel.font = [UIFont systemFontOfSize:14.0f];         //设置button显示字体的大小
    
    [nextButton setBackgroundImage:[UIImage imageNamed:@"button_3"] forState:UIControlStateNormal];    //设置button背景显示图片
    
    [_bgScrollView addSubview:nextButton];
    
    [nextButton addTarget:self action:@selector(confirmMessge:) forControlEvents:UIControlEventTouchUpInside];

}
-(void) initLabel:(int)distance
{
    UILabel *label_user = [[[UILabel alloc]initWithFrame:CGRectMake(0, 10, 180, 30)]autorelease];
    [label_user setText:[NSString stringWithFormat:@"户号 :%@",electricity_model.account]];
    label_user.backgroundColor = [UIColor clearColor];

    [_bgScrollView addSubview:label_user];

    UILabel *label_tip = [[[UILabel alloc]initWithFrame:CGRectMake(0,distance+100, 180, 20)]autorelease];
    label_tip.font = [UIFont fontWithName:@"Arial" size:12];   //设置内容字体和字体大小
    label_tip.text = @"点击每项进入电费详单";
    label_tip.backgroundColor = [UIColor clearColor];

    [_bgScrollView addSubview:label_tip];

}

-(void) initTableView:(NSArray*)namelist
{
    if( [namelist count] > 0)
    {
        UIButton *bgBtn =[UIButton buttonWithType:UIButtonTypeCustom];
        bgBtn.frame = CGRectMake(10, 46, 300, HEIGTH);
        [bgBtn setBackgroundImage:[UIImage imageNamed:@"filmd_02"] forState:UIControlStateNormal];
        [_bgScrollView addSubview:bgBtn];
        
        for( int j=0;j<2;j++)
        {
            UILabel *head = [[UILabel alloc]initWithFrame:CGRectMake(j*150, 0, 150, HEIGTH)];
            head.backgroundColor = [UIColor clearColor];
            head.textColor = [UIColor redColor];
            if( j == 0)
                head.text = @"月份";
            else
                head.text = @"金额";
            head.font = [UIFont fontWithName:@"Arial" size:15];   //设置内容字体和字体大小
            head.textAlignment = NSTextAlignmentCenter;
            head.layer.borderWidth = 0.5f ;
            [bgBtn addSubview:head];
            [head release];
        }
        
        
    }
    
    for (int i  = 0; i < [namelist count]; i ++) {
        UIButton *bgBtn =[UIButton buttonWithType:UIButtonTypeCustom];
        bgBtn.frame = CGRectMake(10, 46+HEIGTH+i*HEIGTH, 300, HEIGTH);
        [bgBtn setBackgroundImage:[UIImage imageNamed:@"filmd_02"] forState:UIControlStateNormal];
        bgBtn.tag = 300+i;
        [bgBtn addTarget:self action:@selector(gotoDetail:) forControlEvents:UIControlEventTouchUpInside];
        [_bgScrollView addSubview:bgBtn];
        
        for( int j=0;j<2;j++)
        {
            UILabel *head = [[UILabel alloc]initWithFrame:CGRectMake(j*150, 0, 150, HEIGTH)];
            head.backgroundColor = [UIColor clearColor];
            head.textColor = [UIColor redColor];
            if( j == 0)
            {
                ElectricFeeInfo* info = [namelist objectAtIndex:i];
                head.text = info.billMonth;
            }
            else
            {
                ElectricFeeInfo* info = [namelist objectAtIndex:i];
                float currentFee =  [info.currentFee floatValue]/100;
                head.text = [NSString stringWithFormat:@"%.2f",currentFee];
            }
            head.font = [UIFont fontWithName:@"Arial" size:15];   //设置内容字体和字体大小
            head.textAlignment = NSTextAlignmentCenter;
            head.layer.borderWidth = 0.5f ;
            [bgBtn addSubview:head];
            [head release];
        }
        
    }
}

-(void)gotoDetail:(id)sender
{
    UIButton* btn = (UIButton*)sender;
    if( btn)
    {
        int index = btn.tag - 300;
        ElectricityInfoViewController *detail =[[ElectricityInfoViewController alloc]initWithIndex:index];
        [self.navigationController pushViewController:detail animated:YES];
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma datasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    printf("count is:%d\n", [electricity_model.electricFeeInfoArray count]);

    return [electricity_model.electricFeeInfoArray count]+1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *oneCell = [tableView dequeueReusableCellWithIdentifier:@"mycell"];
    
    if(oneCell == nil)
    {
        oneCell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"mycell"] autorelease];
        oneCell.frame = CGRectMake(0, 0, 300, 40) ;
        for(int i = 0 ; i < 2 ;i++)
        {
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(oneCell.frame.size.width/2*i, 0, oneCell.frame.size.width/2, oneCell.frame.size.height)];
            label.textColor = [UIColor blackColor];
            label.backgroundColor = [UIColor clearColor];
            label.textAlignment = NSTextAlignmentCenter ;
            label.tag = 170 + i;
            label.layer.borderColor = [UIColor grayColor].CGColor;
            
            label.layer.borderWidth = 0.3 ;
            [oneCell addSubview:label];
        }
    }
    for(int i = 0 ; i < 2 ;i++)
    {
        UILabel *label = (UILabel *)[oneCell viewWithTag:170 + i] ;
        label.backgroundColor = [UIColor clearColor];
        if( indexPath.row == 0 )
        {
            if( i == 0)
                label.text = @"月份";
            else
                label.text = @"金额";
        }
        else
        {
            ElectricFeeInfo* info = electricity_model.electricFeeInfoArray[indexPath.row-1];
            if(i ==0)
            label.text = info.billMonth;
            else
                label.text = info.totalFee;
            NSLog(@"lable.text:%@\n", label.text);
        }

        
        
        
    }
    oneCell.layer.borderColor = [UIColor grayColor].CGColor;
    return oneCell ;
}

-(void)confirmMessge:(id)sender
{
    ElectricityDetail2ViewController *detail = [[[ElectricityDetail2ViewController alloc]init]autorelease];
    detail.account = self.account;
    [self.navigationController pushViewController:detail animated:YES];
}

-(void) dealloc
{
    [cellLabelArray release] ,cellLabelArray = nil;
    [super dealloc];
}

@end
